package platform.udp.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import platform.common.CommonInfo;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

public class NettyClient {

    public static void main(String[] args) {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioDatagramChannel.class).handler(new MyChannelInitializer());
            Channel channel = bootstrap.bind(8083).sync().channel();
            // 向目标端口发送消息
            channel.writeAndFlush(
                    new DatagramPacket(
                            Unpooled.copiedBuffer("nihai ", Charset.forName("GBK")), new InetSocketAddress(CommonInfo.url, CommonInfo.port))).sync();
            channel.closeFuture().await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
}
